<?php
class sledger extends head {

 public $showtype = 1;
 
 function sledger() {
  $this->table = "sledger";
  $this->getmetadata();
  $this->maintainhist = false;
  $this->header[2] = "";
  $_SESSION[strtoupper($this->table)] = $this;
  $_SESSION["sledger"] =$this;
 }
 
 function applyrefs() {
  parent::applyrefs();
  if ($this->header[5] !== "") {
    $desc = freaditem("customer",$this->header[5]);
    $this->refs[5] = $desc[1];
  }
 }
 
 
 function createblank() {
  $this->header = array_fill(0,$this->atts,"");
  $this->header[2] = "0";
  $this->valids = array_fill(0,$this->atts,"");
  $this->lists = array_fill(0,$this->atts,"");
  $this->refs = array_fill(0,$this->atts,"");

  
  $this->header[18] = dtoiso("");
  $user = authenticate(0);
 }
 
 function applyfromrequest() {
  // Mapped requests F1-> field 1 etc, L1->Line 1 etc
  
  $this->valid = true;
  $this->valids = array_fill(0,$this->atts,"");  
  
 }
 
 function newcounter() {
  $ans =  getcounter("SL",1);
  return $ans;
 }
 
 function shownote() {
  echo "<br>";
  echo "<table border=0 cellpadding=0 frame=void><tr>";
  echo "<td><a accesskey='h' onclick='document.form.showtype.value=1;document.form.submit()'>Show lines</a></td>";
  echo "<td><a accesskey='l' onclick='document.form.showtype.value=2;document.form.submit();'>Delivery Address</a></td>";
  echo "</tr></table>";
  echo "<body onload='document.form.f19.focus()'>";
  echo "<h3>Note</h3>";
  echo "<textarea name=f19 accesskey=T cols=40 rows=6>";
  echo $this->header[19];
  echo "</textarea>";
  echo "<br>";
  echo "<input class=bt accesskey=A type=submit name=sub value=Apply>";
  echo "<input class=bt accesskey=S type=submit name=sub value=Save>";
  echo "<input class=bt accesskey=P type=submit name=sub value=Print>";
  echo "<input class=bt accesskey=E type=submit name=sub value=Email>";
  echo "<button class=bt onclick=rest()><span>Restart</span></button>"; 
 }
 
 
 function showform() {
  $h = $this->header;
  $m = $this->metadata;
  $e = $this->valids;
  $c = $this->currentline;
  $le = $this->currentlinevalids;
  $focalpoint = "f5";
  if ($h[5] == "" || $e[5] != "") {$focalpoint = "f5";}
  elseif ($h[1] == "" || $e[1] != "") {$focalpoint = "f1";}
  elseif ($h[17] == "" || $e[17] != "") {$focalpoint = "f17";}
  elseif ($c[5] == "") {$focalpoint = "l5";}
  echo "<head>";
  echo "<script src='datechooser.js' type='text/javascript'></script>";
  echo SCRIPTS ;
  // User enters either a product code or description. Script to switch.
  echo "<script>";
  echo "function dofocus() {";
  echo "if (document.form.l5.value == '' && document.form.l2.value == '') {";
  echo " document.getElementById('idl5').readOnly=false;";
  echo " document.getElementById('idl2').readOnly=false;";
  echo "} else if (document.form.l5.value == '' && document.form.l2.value != '') {";
  echo " document.getElementById('idl5').readOnly=true;";
  echo " document.getElementById('idl2').readOnly=false;";
  echo "} else if (document.form.l5.value != '') {";
  echo " document.getElementById('idl2').readOnly=true;";
  echo " document.getElementById('idl5').readOnly=false;";
  echo "}";
  
  echo "}";
  echo "</script>";
  //
  echo "</head>";
	echo "<body onload='document.form.".$focalpoint.".focus()'>";
  echo "<h1>".$this->doctypes[$this->header[21]]." Entry</h1>"; 
  echo "<form name=form method=POST>";
  echo "<input type=hidden name=x value=-1>\n";
  echo "<input type=hidden name=editline value=-1>";
  echo "<input type=hidden name=res value=''>";
  echo "<input type=hidden name=showtype value=-1>";
  echo "<table>";
  
  // $h[2]. Blank is uninitiated. Zero is new. Anything else is editing.
  echo $this->htmlrow("21","Document Type"); 
  if ($h[2] !== "0") {
   echo "<tr>";
   echo " <td>Order Number</td>";
   echo " <td>$h[0]</td>";
   echo "</tr>";
  }
  echo $this->htmlrow(5,"Customer");
  echo $this->htmlrow(1,"Order Number");

  // Includes callout date selector
  echo "<tr><td>Delivery Date</td><td>";
  $dt = isotoout($this->header[17]);
  echo "<input $this->disabled id=dot name=f17 size=10 maxlength=10 type=text value='$dt'>\n"; 
  if ($this->disabled != "disabled") {
   $dc = "showChooser(this, 'dot', 'chooserSpan', 1950, 2010, 'd-m-Y', false);";
   echo "<img src='calendar.gif' onclick=".'"'.$dc.'"'.">\n";
   echo "<div id='chooserSpan' class='dateChooser' style='display: none; visibility: hidden; width: 160px;'>\n";
   echo "</div>\n";
  }
  echo "</td>";
  echo "<td>".$this->valids[17]."</td>";  
  echo "</tr>";

  echo "</table>";
  
  isset($_REQUEST["showtype"]) ? $newshow = $_REQUEST["showtype"] : $newshow = -1;
  
  // Cannot move on while there are errors
  if ($this->valid) {
   if ($newshow > 0 && $newshow < 10) {
    if ($this->currentline[1] == sizeof($this->lines)) {
     if ($newshow == 1 || $newshow == 2 || $newshow == 3) {
       $this->showtype=$newshow;
     }
    } else {
     $this->showtype=$newshow;
    }
   }
	}
	
  if ($this->showtype==2) {$this->showaddress();
  } elseif ($this->showtype==3) {$this->shownote();
  } elseif ($this->showtype==4) {$this->lineoptions();
  } elseif ($this->showtype==5) {$this->linenote();
	} else {$this->showlines();
  }
  
  echo "</form>"; 
  // set up help
  echo($this->registerhelp());
 }

}
?>